Solving the Mystery of Sensor Value Transmission

Hi everyone, I’ve been diving into the world of home automation lately and I must say, it’s been quite an adventure! :male_detective::male_sign: One of the challenges I’ve encountered is getting sensor values to communicate effectively with my scripts. Specifically, I’ve been working with a sensor that’s supposed to pass room selection data to a cleaning script, but it wasn’t quite working as expected. Let me walk you through my journey and how I finally cracked the code!

Initially, I set up a sensor using the template platform in Home Assistant. The idea was to have it collect data from a group of rooms and convert them into a specific format that my vacuum cleaner could understand. Here’s a snippet of what I had:

yaml
sensor:

  • platform: template
    sensors:
    select_rooms_for_clean:
    value_template: >
    {% set dfg = expand(‘group.all_rooms_for_vac_clean’) | selectattr(‘state’, ‘eq’, ‘on’) | map(attribute=‘entity_id’) | list | join(‘,’) | replace(‘input_boolean.bedroom_vacuum’,‘14’) | replace(‘input_boolean.kinozal_vacuum’,‘12’) | replace(‘input_boolean.hallway_vacuum’,‘17’) | replace(‘input_boolean.cabinet_vacuum’,‘10’) | replace(‘input_boolean.kitchen_vacuum’,‘11’) %}
    {{dfg}}
    friendly_name: “Rooms”
    icon_template: mdi:vacuum

The goal was for this sensor to pass its value to a script that would trigger the vacuum cleaner to start. But here’s where I hit a snag: the script wasn’t receiving the sensor’s value correctly. I kept getting errors, and the vacuum wouldn’t start as intended. Frustrating, to say the least! :sweat:

After some research and trial and error, I realized the issue was with how the sensor’s value was being passed to the script. I was using states('sensor.select_rooms_for_clean') in the script, but it wasn’t capturing the value properly. I needed a different approach to ensure the sensor’s value was correctly transmitted.

Here’s what I did to fix it:

  1. Script Modification: I revised my script to ensure it correctly references the sensor’s state. Instead of directly using states(), I made sure to structure the script so that it could reliably pull the sensor’s value.

yaml
script:
clean_select_rooms:
alias: Clean selected
sequence:
- service: xiaomi_miot.call_action
data:
entity_id: vacuum.robot_cleaner
siid: 7
aiid: 3
params:
- !state sensor.select_rooms_for_clean
- 0
- 1

  1. Testing and Validation: I tested the setup multiple times, manually triggering the sensor and observing the script’s behavior. It was crucial to ensure that the sensor’s value was being updated and transmitted correctly each time.

  2. Community Support: I also reached out to the community for advice. Someone suggested checking the sensor’s state in the developer tools to ensure it was updating as expected. This simple check helped me confirm that the sensor itself was working fine, and the issue was indeed with the script.

After making these adjustments, the system started working like a charm! :tada: Now, when I select rooms via the sensor, the vacuum cleaner starts cleaning those specific areas without any hiccups. It’s such a relief to have this functionality up and running smoothly!

This experience taught me the importance of thorough testing and the value of community support when troubleshooting. If anyone else is struggling with similar issues, don’t hesitate to reach out—I’m happy to help! :bulb:

Happy automating, everyone! :rocket: