I’m still doing some experiments and test with my ESP8266 via REST API.
I added temperature and humidity sensors and a switch to toggle ON/OFF a simple led.
Yesterday I succesfully connected a PIR sensor and wrote code to detect presence or not and respective REST API which GET returns this JSON:
{"presence_detected" : true}
Now I wanto to create a simple automation with PIR sensor and the led to better understand automations, triggers, actions, etc.
Something like “PIR detect presence > turn lights on” (in this case lights is the LED).
Can someone help me? I read documentation but I didn’t understand very well how it works.
I think I have a little bit confusion about some configuration elements and terms.
For example:
what a sensor state is exactly? Is its latest value retrivied by HASS? Or it could be overrided and set a custom value into HASS according which sensor value?
Example:
I have a temperature sensor: as far as I understand it’s state is the temperature in degrees.
Can I set a custom state like this? (pseudo-code) state = if( temperature > 30 ){ "hot" } else {"cold"}
On your trigger, it was reported:
state: 'motion detected'
so as far as I understand you have a sensor which return that state?
In my case it should be “true” or “false”?
STATE is the current state of any object. It could be a sensor or a switch, or really anything.
You CAN set the state of objects, but if an object has a default method for getting its state, then that would over-ride anything you write.
FOR EXAMPLE
If you have a temperature sensor that gets its value from a physical sensor, you COULD over-write that value, but the next update from the physical sensor would over-write your value (and those updates come unbelievably fast).
If you really want a sensor that has values of “hot”, “cold” based on the temperature of your physical temperature sensor, then create a new sensor (they don’t cost anything!) Make your new sensor a template sensor and set it’s value to “hot” or “cold” based upon the state of the temperature sensor.
{%if states.sensor.garage_temperature.state | int > 30 %}
hot
{%else%}
cold
{%endif%}
light.sn2_led is in the light domain.
light objects function somewhat differently than switch objects, but both domains have a turn_on service.
So, in my case, the PIR sensor state is retrieved with GET API, which return JSON: {"presence_detected" : true}
so the state should be true or false right? (how to code it into yaml? true or "true"?)
So my automation will be something similar to:
automation:
- alias: 'Turn LED on on motion'
trigger:
- platform: state
entity_id: sensor.sensor_test_motion
state: true
action:
service: homeassistant.turn_on
switch.switch_test
With code poste by @treno automation seems works fine.
BTW I cannot understand this two issues:
i must escape true/false values as string
the values detected by the PIR sensor JSON are ‘True’ and ‘False’ and not ‘true’ and ‘false’
The JSON contains a boolean, and not a string, in fact it is {"presence_detected" : true}
not {"presence_detected" : "true"}
Should I return a string as state instead of bool?
To get a good understanding of how this works, go to the template tool under developer tools and type something like this {{states.binary_sensor.garage_motion_detected.state == 'false'}}
You can play with your values and see the results. `
How does your sensor and mqtt declaration look? I’m also using the bruh multisensor.
I followed his example right off. Works fine but I’m also having problems using them with automations.