I have three sensors (1 ultrasonic, 1 luxmeter, 1 hue motion sensor) and script that is supposed to run if values from the sensors are matching a condition specified in a lambda. If they evaluate to true the script is supposed to run. However, ESP is giving some error when compiling. I’ve tried enveloping the “off” in “state.== off” in quotes, and without.
Try replacing id(hue_motion_1).state == off with !id(hue_motion_1).state
The ! is equivalent to checking if it is false (off) in this case
Pointers always trip me up too. The above worked for me but I was also assigning the state to a variable (for other reasons) and checking on the variable so it may be different than your case but worth a try while you wait for a more experienced programmer to respond.
Thanks. It compiles without errors now. However, it doesn’t actually do anything to interrupt the automation. I think it’s somehow having problems reading the state of the motion sensor in this part:
A pointer is a variable that stores the memory address of another variable as its value which is why the comparison between int and pointer was failing. I am guessing that ESPHome converts “off” to 0 (false) so it sees it as an int.
Assuming that my suggestion is correct, have you verified that your states are what you think they should be? I would try to log them so you can see what the values are.
If it doesn’t work, you can check out this thread as it has some good pointers (no pun intended):
The thread also talks about specifiers %d in your case which is used for int. If you want to log other values such as a string you would use ‘%s’ as a specifier.
I do a lot of this stuff in Lambda so I never actually tested the logger.log function as above. I typically use something like this in my Lambda:
Please note I hack my way through so I have no certainty that I am giving you “state of the art” programming tips… but they have worked for me so I hope they help you out.
Thanks. I tried pasting your cod but got an error, so instead, I moved the logger part to the binary sensor itself. Moving it didn’t give me an error but also didn’t actually generate any info in the output
I got some help from the people over at Reddit which solved the conditional lambda issue by switching to this:
id(hue_motion_1).state == false;'
or similar to your suggestion (what :
!id(hue_motion_1).state == false;'
However. The script is either unaffected, or doesn’t execute at all irrespective of motion or not. I think the logging function could provide valuable insight here if I can solve it.
That is precisely what I want. I just tested with the motion on and off. So one scenario was just triggering motion while the luxmeter is covered (and well below 200 lx). Will ESP automatically update the state of the motion sensor or do I have somehow have to force a regular update?
The log entries unfortunately don’t show anything for the motion. For the luxmeter and ultrasonic sensor it shows reading normally.
That reminds me… I would avoid using a motion sensor that is not directly connected to the ESP as I see it as a significant point of failure. I would want the ESP to keep working regardless of what is going on with HA or the connection between the two. Anyhow, while I have not tried to do it, I don’t see why it would not work.
It sounds like the script is not running or is stopped too soon. Looking at the code it appears you call the script when the Ultrasonic sensor detects a value under 0.5 (meters right?) and then stop the script immediately after the if.
I just noticed a likely error… the script.stop in your code executes every time right after the if but given you have an else: just above it I’d say your indentation is incorrect. I added 6 spaces to align it where it should be if meant to run only when the “ultra” condition if false. Do you even need to stop the script? I thought stopping the script was only necessary when it did something that took a long time, and may therefore need to be stopped, which does not appear to be your case.
Thanks very much for your help. If I don’t have the stop script it just keeps looping even when the conditions are false, for some reason. However, I did something similar to what you did but moved the stop script to the script itself.
After a long and arduous day of ripping my hair out, I think we zeroed in on the main problem almost at the same time. Direct connection (i.e. API).
My HUE is located on the same net as the ESP, but different from the RBPi. I run almost everything else over MQTT, but I realised just now that my HUE doesn’t. Tomorrow I will attempt to set up Zigbee2MQTT.
Until then, I setup an experiment using mqtt subscribe in order to test my hypothesis ;
Manually switched on the esp32_transistor_25 in order to push a state change
Result: It indeed evaluated the expression "== “OFF” as false and stopped the script from running! (Switching off the esp32_transistor_25 allowed the script to resume and it ran just fine).
I can also confirm that the problem very likely is due to not have the API connection. I’ve created an automation that switches on a switch over in the same ESP as the ultra/lux sensor over MQTT when the motion detects motion and it correctly stops the script from running.