Setting a variable based on state of Tuya device


I have several use cases where I want to change the state of a Tuya device when something happens (e.g. set a light to a specific brightness when a door sensor triggers ‘open’), but then restore the original state when something else happens (e.g. set the light back to the original brightness when the door sensor triggers ‘closed’).

My question is, how can I capture the brightness value from the device’s properties into a variable in a logicblock? I can use a Webhook to set a variable using POST and setting parameters, but I can only reference values of existing variables in the Value field, which frankly is pretty useless if you can’t dynamically set a variable based on the reading/property of a device.

Is there another way to reference the property of the device? Is there some secret JSONy/scripty magic like using {{TuyaDeviceName.Brightness.value}} or something? Is what I want to do even possible? PS: I’m trying to avoid using IFTTT as part of the logic in Apilio as the lag/latency in actions can be close to a minute which is, again, pretty useless. Given that Apilio can clearly capture state changes on Tuya devices (as you can see in logs) then why not allow us to capture those changes into variables to use later?



I think this is high on many people’s wish-lists, but unfortunately it can’t be done at present. As you say, being able to see the values recorded from a device via the Apilio Web App, but not being able to access these programmatically, is quite frustrating.

1 Like

I feared as much. In that case Apilio isn’t really useful for me. The only things I’ve been able to do with it are things I can do in other ways anyway. State machines in the automation logic is the only thing I can’t do without resorting to workarounds (e.g. using Google Sheets with IFTTT to store state) that are waaaay to slow to execute to be usable, because of the polling mechanisms involved). I’m looking for true event driven state management.

1 Like

@robin said it right, that is currently not possible.
Definitely something we’d like to add as well!

1 Like

This is an interesting problem that is similar to something I’ve had to deal with for reading thermostat temperatures, so some thoughts are below.

A possible work-around, providing you are willing to accept a slightly limited set of brightness values, could be as follows (I’m assuming that your lights have brightness values ranging from 10-1000 for their “Bright value v2”-- if they use 1-100 then adjust the logic accordingly):

Define a numeric variable ‘light_brightness’ and a boolean variable ‘record_light_brightness’ (initially TRUE)

Set up the following Tuya conditions that use ‘inside range’ and are set to trigger a logicblock when the light brightness changes to be within a certain range.

Condition, range

  • bright1, 10-100
  • bright2, 100-200
  • bright3, 200-300
  • bright10, 900-1000

Set up a further condition ‘is_recording_brightness’ that depends on whether ‘record_light_brightness’ is TRUE.

Set up the following logicblocks

Logicblock, triggers

  • handle_bright1, bright1 and is_recording_brightness
  • handle_bright2, bright2 and is_recording_brightness
  • handle bright10, bright10 and is_recording_brightness

Each logicblock is only triggered if the brightness is within the defined range, and only if ‘is_recording_brightness’ is TRUE. For each logicblock, update the variable light_brightness using a webhook to an appropriate level. So, for handle_bright2 you could set it to 150, etc.

When your door sensor triggers “open”, first set ‘recording_brightness’ to FALSE (to prevent further updates to ‘light_brightness’), then change the lamp brightness to the level you require.

When the door sensor triggers “closed”, set ‘recording_brightness’ to TRUE. You then need to restore the saved value for 'light_brightness. However, here we have an issue as although you can set the brightness level of the Tuya light to any allowed explicit value, you cannot pass a variable value direct.

You can, however, get around this using IFTTT providing you can also set the light brightness from IFTTT. Create an IFTTT aplet that is triggered from Apilio and passes the ‘light_brightness’ value (pass as “{{light_brightness}}” in the advanced option settings in the Apilio IFTTT call). Then use this value within IFTTT to set the light brightness. If you call this IFTTT aplet from Apilio as the final step in the logicblock that triggers when the door sensor is closed, then you should get the effect you desire. The slight latency when calling IFTTT would not be a major issue here as it would only occur when the door sensor closes.

The granularity of the light brightness could be varied depending on how many conidtions and logicblocks you want to use. I’ve suggested 10, but you could increase this if you wished.

**Note for @pebneter **: it would be really helpful if Tuya devices could be passed a value that derives from a variable, rather than needing an explicit value. So, for example in this instance, if a Tuya light could be passed a value such as “{{light_brightness}}” (as you can do when passing values via IFTTT) then you would not need to go via IFTTT to set this.


Thanks for taking the time to detail that out Robin. I had a similar thought last night. “Actually, how many different brightness values do I really use?”

For some cases, your solution would work for me, although I’d still prefer to not have to punch out to IFTTT as it’s just one more point of failure / latency. It will work, but less reliably than I’d like.

In other cases, I need to reset the Tuya parameters back to EXACTLY where they were after a temporary change. Brightness isn’t so bad, although my other half is someone susceptible to noticing when things aren’t to her liking and complains that “the stupid smart stuff doesn’t do what I want”. :slight_smile: For colours however, when we set very specific RGB values, I want to automate them returning to those values after they’ve been temporarily changed in some cases. Setting ranges and then returning to some point within the range doesn’t cut it.

Setting variables to the value of a Tuya parameter within Logicblocks is the only real answer. As a software developer and technical/cloud architect of many many years, I know this wouldn’t be that hard to implement as the data models are already there and used in other parts of the Apilio front-end.

1 Like

Furthermore, while I get the drivers for the concepts of variables, conditions and logicblocks, for a lot of use cases it does feel like a somewhat unnecessary level of abstraction (and overcomplication for users). It occurs to me that the design decisions for going down that route would make a lot more sense if the scope (in a functionality sense) of variables was planned to be greater than it is already…

So my question for @pebneter is, can you share your product roadmap somewhere on your site please? Lots of these community posts have comments from Apilio staff saying “it’s something we’d like to do”, but at the moment, I’m probably not going to buy into this ecosystem as it’s too limited. I might if I can see a feature roadmap.

1 Like

As someone who has no affiliation with Apilio, I think you are being somewhat harsh and should try it a little longer. After all, alternatives are essentially non-existent, and the Apilio ecosystem is remarkably robust and responsive. Not having access to persistent variables is a huge limitation of IFTTT, so Apilio would be worthwhile even if it was just an add-on to IFTTT that provided a (very responsive) data repository. Indeed, when I first started using Apilio I viewed it as exactly that, working on the basis that I would do most things in IFTTT with some help from Apilio. However, after working with it for a while, I now take essentially the reverse position, with Apilio being the main engine but with IFTTT providing additional functionality where Apilio is lacking.

The other thing that is different about Apilio is that the developers are remarkably open to suggestions – just look at this forum and see how often this happens. Good luck trying to get responses from developers at IFTTT / Smartthings / Google / Alexa / Tuya… I am also a software developer of many years standing, and it is very refreshing to have real developers (such as @pebneter ) take time to respond, as they do here.


I don’t really use IFTTT very much because that too is quite a limited ecosystem. LOTS of integrations, but limited in what you can really do with them in a practical sense. After all these years, it still feels like a PoC.

Apilio seems to have moved on quickly and I do see the developer interaction with users, and I like that! All I was saying is that for my personal use cases, Apilio is still quite limited although clearly it was intended to build on the IFTTT ecosystem (which I do not care for) and address its shortcomings. All I was asking for is a view of where things are going with Apilio so I can decide whether to look for something else and invest time in that, or hang on for this! No offence intended at all. Add in variable assignations from other properties rather than static values within Apilio itself and Apilio ticks all my boxes! If that’s coming in the next weeks or months - I’m all in.

My alternative is to create something for myself that gives me ultimate flexibility, which I have the skills to do, but I simply do not have the time.

1 Like

Wow, great thread - thanks all - and I think it seems a very reasonable request to share a road map. We’ll get to work on it and publish for comments/thoughts.