Map values for an input_select?

So put the mapper code in the id: section? Cool! I got it right. :slight_smile: However, I overlooked the detail of changing data to data_template. I’ll eventually get the hang of it.

But I do like petro’s suggestion to move the mapping to a separate sensor. Makes it re-usable for other automations and makes the automation’s code neater.

Ideally, input_select would support a list of key-value pairs (list of lists). Then it could effectively store the mapping and you could identify the selected item either by key or value. Of course, that would be a heckuva breaking change!

options:
- - Corridor
  - '5'
- - Corridor terrace
  - '7'
- - Master bedroom
  - '12'
- - Corridor Office
  - '22'
- - Control room
  - '28'
- - Attic
  - '33'

Yah, it would be nice to have a map. I would theroize it would look something like this (to correctly follow yaml rules)

  options:
    - option: Corridor
      value: '1'
    - option: Corridor terrace
      value: '7'
    - option: Master bedroom
      value: '12'
    - option: Corridor Office
      value: '22'
    - option: Control room
      value: '28'
    - option: Attic
      value: '33'

And if this is done, I think the changes could be backward compatible. I.E. Under the hood this is a list of dictionaries where the old method is a list of strings. You can easily tell between the 2.

I think this would make a great feature request. Not sure if it would get much traction though.

3 Likes

It would certainly put the mapping where it belongs, namely with the selector (the source of truth).

I like your version (option:/value:) better. I used ‘list of lists’ style but it looks awkward and may be more error-prone when indenting.

1 Like

Yeah, people would get confused too. That’s usually the biggest issue. And yaml is confusing enough!

1 Like

I’ll submit it in the Feature Requests section. At the very least, the idea will be documented.

3 Likes

ill vote for it

1 Like

well, tbh, Im not sure…
I could imagine this would work in the backend, but my idea of the mapper is to keep backend stuff in the backend, and make the frontend as humanly understandable as possible. Don’t want the id’s in the frontend, but only then names…

If this mapper would enable that, Id vote for it :wink:

have another feature request though, that is, for my automations…

if Id like to set the sensitivity for a group of lets say 5 sensors in a group, or individual, to medium (1) how could I do that without having to select 5 sensors individually.

Could we make a loop (for sensors 1-5) or for sensors id in group.hallway_motion_sensors. etc etc?

now thats a challenge with the above automation, which might even turnout to become a script

input select: sensitivity
input select: sensors
script: rest_command.hue_command

Yes, that’s the goal. Referring to petro’s example, only option: would be shown in the selector and not value:.

In the home automation system I use now (Premise), it lets you create the mapping as a separate entity (called an enumeration). The enumeration contains child objects called enums. An enum has a name and a value (and more).

When you create a multivalue selector you specify which enumeration it should use. So, technically speaking, the mapping is not stored with the selector but stands alone. You just tell the selector which mapping (enumeration) it should use.

This illustrates the relationship between a selector (OccupancyMode), its enumeration (OccupancyModes), and the enums in the enumeration (Day, Night, Away, Vacation).

I don’t think Home Assistant needs this level of flexibility. Just being able to have a map defined with the selector is a big improvement.

1 Like

yes, that is clear.
I don’ t feel to miss out on any of that though in HA, maybe because I am so used to these mappers… Simple forgot to put it in the action…

btw, when it boils down to using the mapper directly, or via an intermediary template sensor, I ,for now, prefer the former, to keep things together. And, avoid as many extra states/entities as possible, having over 1600 of them already…

If at one point Id be able to use a template elsewhere that might change of course. Just as the rest command is a template that can be used in other situations, kind of a module so to speak.

try {{states|count}} in dev-template :wink:

well, ive taken it this far for now. Somehow the command doesn’t work yet. I think the template for data {"sensitivity": "{{states('input_select.set_hue_sensitity')}}" } is causing that, or the fact that my secret is not allowing the variables.

this is in the secrets file:

url_set_hue_command: http://192.168.1.212/api/API/{{ type }}/{{ id }}/{{ command }}

ill give it a nights sleep, please check with me what cold be wrong?

18
##############################################################################################################
# Package Hue commands by @mariusthvdb
# see community discussion on Map values for an input_select?
# and Hue motion sensors + remotes: custom component
# big help by @pnbruckner @Martso @cicero222 and @petro. @123 file a feature request for the mappings on
# Input_select enhancement. Support mapping
# jan 25 2019
##############################################################################################################

homeassistant:
  customize:
    script.set_hue_command:
      action_name: 'Set sensitivity'
      icon: mdi:settings

    input_select.set_hue_sensitivity:
      templates:
        icon: >
          if (state === '0') return 'mdi:numeric-0-box-multiple-outline';
          if (state === '1') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === '0') return 'grey';
          if (state === '1') return 'blue';
          return 'red';

##############################################################################################################
# Scripts and Commands
##############################################################################################################
# https://community.home-assistant.io/t/hue-motion-sensors-remotes-custom-component/27176/698?u=mariusthvdb
rest_command:
  set_hue_command:
    url: !secret url_set_hue_command
    method: put
    payload: '{{ data }}'

script:
  set_hue_command:
    alias: Set Hue command
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        data: >
          {"sensitivity": "{{states('input_select.set_hue_sensitity')}}" }
        id: >
          {% set mapper =
            { 'Laundry':'22',
              'Dining table':'52',
              'Auditorium':'44',
              'Frontdoor':'60',
              'Dorm':'56',
              'Corridor':'5',
              'Corridor terrace':'33',
              'Master bedroom':'48',
              'Corridor Office':'28',
              'Control room':'7',
              'Attic':'12' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}
        type: sensors

##############################################################################################################
# Inputs
##############################################################################################################

input_select:
  set_hue_sensitivity:
    name: set Hue sentitiviy
    options:
      - '0' #low
      - '1' #medium
      - '2' #high

  select_hue_motion_sensor:
    name: Select motion sensor
    icon: mdi:run-fast
    options:
      - Laundry
      - Dining table
      - Auditorium
      - Frontdoor
      - Dorm
      - Corridor
      - Corridor terrace
      - Master bedroom
      - Corridor Office
      - Control room
      - Attic

##############################################################################################################
# Groups
##############################################################################################################

group:
  set_hue_sensor_sensitivity:
    icon: mdi:run-fast
    control: hidden
    name: Set Hue sensor sensitivity
    entities:
      - input_select.select_hue_motion_sensor
      - input_select.set_hue_sensitivity
      - script.set_hue_command

The package refers to the selector using three different versions of the word 'sensitivity`.

In customize:

input_select.set_hue_sensitivity: <--- sensitivity

In script:

{"sensitivity": "{{states('input_select.set_hue_sensitity')}}" } <--- sensitity

For input_select:

name: set Hue sentitiviy <--- sentitiviy

good catch!
didn’t help though…
test results so far:

secret with variable to secrets file: check, working fine (hadn’t done that before, so good to know)
template for id: check! working fine.

now testing template for sensitivity chichis a bit more of a challenge, though I see I had quoted the template and it should really be:

      `{"sensitivity": {{states('input_select.select_hue_sensitivity')}} }`

Update

Bingo!!:

I can report Ive got it working as hoped for, and made a few testing sensors to show it does, like:

- platform: command_line
  name: Laundry sensitivity
  command: curl -X GET http://192.168.1.212/api/API/sensors/22
  value_template: >
    {{ value_json.config.sensitivity }}

44
35
36

new package, fully functional, with added sensors and a new Mapper for sensitivity:

##############################################################################################################
# Package Hue sensitivity by @mariusthvdb
# see community discussion on https://community.home-assistant.io/t/map-values-for-an-input-select/94354/5
# and https://community.home-assistant.io/t/hue-motion-sensors-remotes-custom-component/27176/687?u=mariusthvdb
# big help by @pnbruckner @Martso @cicero222 and @petro. @123 filed a feature request for the mappings on
# https://community.home-assistant.io/t/input-select-enhancement-support-maps-key-value-pairs/94391
# jan 25 2019
##############################################################################################################

homeassistant:
  customize:
    script.set_hue_sensitivity:
      action_name: 'Set'
      icon: mdi:settings

    input_select.select_hue_sensitivity:
      templates:
        icon: >
          if (state === 'Low') return 'mdi:numeric-0-box-multiple-outline';
          if (state === 'Medium') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === 'Low') return 'grey';
          if (state === 'Medium') return 'blue';
          return 'red';

    sensor.laundry_sensitivity:
      templates:
        icon: >
          if (state === '0') return 'mdi:numeric-0-box-multiple-outline';
          if (state === '1') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === '0') return 'grey';
          if (state === '1') return 'blue';
          return 'red';

    sensor.auditorium_sensitivity:
      templates:
        icon: >
          if (state === '0') return 'mdi:numeric-0-box-multiple-outline';
          if (state === '1') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === '0') return 'grey';
          if (state === '1') return 'blue';
          return 'red';

##############################################################################################################
# Scripts and Commands
##############################################################################################################
# https://community.home-assistant.io/t/hue-motion-sensors-remotes-custom-component/27176/698?u=mariusthvdb
rest_command:
  set_hue_command: #not named sensitivy because the command can be used to set other entities/attributes also
    url: !secret url_set_hue_command
    method: put
    payload: '{{ data }}'

script:
  set_hue_sensitivity:
    alias: Set Hue sensitivity
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {% set mapper =
            { 'Low':'0',
              'Medium':'1',
              'High':'2' } %}
          {% set state = states('input_select.select_hue_sensitivity') %}
          {% set sensitivity = mapper[state] if state in mapper %}
          {"sensitivity": {{sensitivity}} }
        id: >
          {% set mapper =
            { 'Laundry':'22',
              'Dining table':'52',
              'Auditorium':'44',
              'Frontdoor':'60',
              'Dorm':'56',
              'Corridor':'5',
              'Corridor terrace':'33',
              'Master bedroom':'48',
              'Corridor Office':'28',
              'Control room':'7',
              'Attic':'12' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}


##############################################################################################################
# Inputs
##############################################################################################################

input_select:
  select_hue_sensitivity:
    name: select Hue sensitivity
    options:
      - Low
      - Medium
      - High

  select_hue_motion_sensor:
    name: select Motion sensor
    icon: mdi:run-fast
    options:
      - Laundry
      - Dining table
      - Auditorium
      - Frontdoor
      - Dorm
      - Corridor
      - Corridor terrace
      - Master bedroom
      - Corridor Office
      - Control room
      - Attic

##############################################################################################################
# Sensors
##############################################################################################################
sensor:
  - platform: command_line
    name: Laundry sensitivity
    command: !secret get_laundry_config
    value_template: >
      {{ value_json.config.sensitivity }}

  - platform: command_line
    name: Auditorium sensitivity
    command: !secret get_auditorium_config
    value_template: >
      {{ value_json.config.sensitivity }}

##############################################################################################################
# Groups
##############################################################################################################

group:
  set_hue_sensor_sensitivity:
    icon: mdi:run-fast
    control: hidden
    name: Set Hue sensor sensitivity
    entities:
      - sensor.laundry_sensitivity
      - sensor.auditorium_sensitivity
      - input_select.select_hue_motion_sensor
      - input_select.select_hue_sensitivity
      - script.set_hue_sensitivity

##############################################################################################################
# Notes
##############################################################################################################

UPDATE

with Tiles, mimicking the Hue App:

42

1 Like

Update on sensor.sensitivity:

attribute sensitivity has been added to the Hue CC https://raw.githubusercontent.com/robmarkcole/Hue-sensors-HASS/master/custom_components/sensor/hue.py

following our discussion on Hue motion sensors + remotes: custom component

and Github https://github.com/robmarkcole/Hue-sensors-HASS/pull/108

so now this template sensor is possible based on the binary_sensor created by the Hue CC:

  corridor_motion_sensor_sensitivity:
    friendly_name: 'Corridor sensitivity'
    value_template: >
      {{state_attr('binary_sensor.corridor_motion_sensor','sensitivity')}}

UPDATE

Added a HA interface to the selection of daylight threshold of the motion sensor in the Hue App, using the same rest command I use for the sensitivity. Which is possible because of the templatable format of the command which takes its variable from a script.

36

31
for completeness sake, here’s the (almost) full package:

##############################################################################################################
# Package Hue Sensitivity and Threshold by @mariusthvdb
#
# see community discussion on https://community.home-assistant.io/t/map-values-for-an-input-select/94354/5
# and https://community.home-assistant.io/t/hue-motion-sensors-remotes-custom-component/27176/687?u=mariusthvdb
# big help by @pnbruckner @Martso @cicero222 and @petro.
# https://community.home-assistant.io/t/input-select-enhancement-support-maps-key-value-pairs/94391
# jan 25 2019
##############################################################################################################

homeassistant:

  customize_glob:
    sensor.*_motion_sensor_sensitivity:
      templates:
        icon: >
          if (state === '0') return 'mdi:numeric-0-box-multiple-outline';
          if (state === '1') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === '0') return 'grey';
          if (state === '1') return 'blue';
          return 'red';

    sensor.*_motion_sensor_threshold:
      icon: mdi:altimeter
      templates:
        icon_color: >
          if (state > 25000 ) return 'rgb(85, 0, 0)';
          if (state > 20000) return 'rgb(255, 0, 0)';
          if (state > 15000) return 'rgb(255, 165, 0)';
          if (state > 10000) return 'rgb(0, 128, 0)';
          if (state > 7500) return 'rgb(255, 211, 30)';
          if (state > 5000) return 'rgb(255, 255, 0)';
          if (state > 25000) return 'rgb(30, 144, 255)';
          if (state > 1000) return 'rgb(30, 255, 255)';
          return 'rgb(47, 0, 0)';

  customize:
    script.set_hue_sensitivity:
      action_name: 'Set'
      icon: mdi:settings

    script.set_hue_threshold:
      action_name: 'Set'
      icon: mdi:settings

    input_select.select_hue_sensitivity:
      templates:
        icon: >
          if (state === 'Low') return 'mdi:numeric-0-box-multiple-outline';
          if (state === 'Medium') return 'mdi:numeric-1-box-multiple-outline';
          return 'mdi:numeric-2-box-multiple-outline';
        icon_color: >
          if (state === 'Low') return 'grey';
          if (state === 'Medium') return 'blue';
          return 'red';

    input_number.slide_hue_threshold:
      templates:
        icon_color: >
          if (state > 25000 ) return 'rgb(85, 0, 0)';
          if (state > 20000) return 'rgb(255, 0, 0)';
          if (state > 15000) return 'rgb(255, 165, 0)';
          if (state > 10000) return 'rgb(0, 128, 0)';
          if (state > 7500) return 'rgb(255, 211, 30)';
          if (state > 5000) return 'rgb(255, 255, 0)';
          if (state > 25000) return 'rgb(30, 144, 255)';
          if (state > 1000) return 'rgb(30, 255, 255)';
          return 'rgb(47, 0, 0)';

##############################################################################################################
# Scripts and Commands
##############################################################################################################
# https://community.home-assistant.io/t/hue-motion-sensors-remotes-custom-component/27176/698?u=mariusthvdb
rest_command:
  set_hue_command: #not named sensitivy because the command can be used to set other entities/attributes also
    url: !secret url_set_hue_command
    method: put
    payload: '{{ data }}'

script:
  set_hue_sensitivity:
    alias: Set Hue sensitivity
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {% set mapper =
            { 'Low':'0',
              'Medium':'1',
              'High':'2' } %}
          {% set state = states('input_select.select_hue_sensitivity') %}
          {% set sensitivity = mapper[state] if state in mapper %}
          {"sensitivity": {{sensitivity}} }
        id: >
          {% set mapper =
            { 'Laundry':'22',
              'Dining table':'52',
              'Auditorium':'44',
              'Frontdoor':'60',
              'Dorm':'56',
              'Corridor':'5',
              'Corridor terrace':'33',
              'Master bedroom':'48',
              'Corridor Office':'28',
              'Control room':'7',
              'Attic':'12' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}

  set_hue_threshold:
    alias: Set Hue threshold
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {"tholddark": {{states('input_number.slide_hue_threshold')|int}} }
        id: >
          {% set mapper =
            { 'Laundry':'25',
              'Dining table':'53',
              'Auditorium':'45',
              'Frontdoor':'61',
              'Dorm':'57',
              'Corridor':'6',
              'Corridor terrace':'34',
              'Master bedroom':'49',
              'Corridor Office':'29',
              'Control room':'9',
              'Attic':'13' } %}
          {% set state = states('input_select.select_hue_motion_sensor') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}

##############################################################################################################
# Inputs
##############################################################################################################

input_select:
  select_hue_sensitivity:
    name: select Hue sensitivity
    options:
      - Low
      - Medium
      - High

  select_hue_motion_sensor:
    name: select Motion sensor
    icon: mdi:run-fast
    options:
      - Laundry
      - Dining table
      - Auditorium
      - Frontdoor
      - Dorm
      - Corridor
      - Corridor terrace
      - Master bedroom
      - Corridor Office
      - Control room
      - Attic

input_number:
  slide_hue_threshold:
    icon: mdi:altimeter
    name: Slide Hue threshold
    min: 0
    max: 30000 #hue max = 65534 but makes slider max ot of scale, since max is almost identical to 30000
    step: 2500
##############################################################################################################
# Sensors
##############################################################################################################
sensor:

##############################################################################################################
# Sensitivity sensors
##############################################################################################################

#not used anymore, replaced by attributes sensitivity in CC HUE binary sensor
#  - platform: command_line
#    name: Laundry sensitivity
#    command: !secret get_laundry_config
#    value_template: >
#      {{ value_json.config.sensitivity }}

#  - platform: command_line
#    name: Auditorium sensitivity
#    command: !secret get_auditorium_config
#    value_template: >
#      {{ value_json.config.sensitivity }}

  - platform: template
    sensors:
      laundry_motion_sensor_sensitivity:
        friendly_name: 'Laundry sensitivity'
        value_template: >
          {{state_attr('binary_sensor.laundry_motion_sensor','sensitivity')}}

      dining_table_motion_sensor_sensitivity:
        friendly_name: 'Dining table sensitivity'
        value_template: >
          {{state_attr('binary_sensor.dining_table_motion_sensor','sensitivity')}}

      auditorium_motion_sensor_sensitivity:
        friendly_name: 'Auditorium sensitivity'
        value_template: >
          {{state_attr('binary_sensor.auditorium_motion_sensor','sensitivity')}}

      # etcetc
##############################################################################################################
# Threshold sensors
##############################################################################################################

  - platform: template
    sensors:
      laundry_motion_sensor_threshold:
        friendly_name: 'Laundry threshold'
        value_template: >
          {{state_attr('binary_sensor.laundry_motion_sensor','threshold')}}

      dining_table_motion_sensor_threshold:
        friendly_name: 'Dining table threshold'
        value_template: >
          {{state_attr('binary_sensor.dining_table_motion_sensor','threshold')}}

      auditorium_motion_sensor_threshold:
        friendly_name: 'Auditorium threshold'
        value_template: >
          {{state_attr('binary_sensor.auditorium_motion_sensor','threshold')}}

      #etcetc
##############################################################################################################
# Groups
##############################################################################################################

group:
  set_hue_sensitivity_sensor:
    icon: mdi:run-fast
    control: hidden
    name: Set Hue sensitivity sensor
    entities:
      - input_select.select_hue_motion_sensor
      - input_select.select_hue_sensitivity
      - script.set_hue_sensitivity

  set_hue_threshold_sensor:
    icon: mdi:altimeter
    control: hidden
    name: Set Hue threshold sensor
    entities:
      - input_select.select_hue_motion_sensor
      - input_number.slide_hue_threshold
      - script.set_hue_threshold

  hue_sensitivity_sensors:
    name: Hue sensitivity sensors
    control: hidden
    icon: mdi:numeric
    entities:
      - sensor.laundry_motion_sensor_sensitivity
      - sensor.dining_table_motion_sensor_sensitivity
      - sensor.auditorium_motion_sensor_sensitivity
      - sensor.frontdoor_motion_sensor_sensitivity
      - sensor.dorm_motion_sensor_sensitivity
      - sensor.corridor_motion_sensor_sensitivity
      - sensor.corridor_terrace_motion_sensor_sensitivity
      - sensor.master_bedroom_motion_sensor_sensitivity
      - sensor.corridor_office_motion_sensor_sensitivity
      - sensor.control_room_motion_sensor_sensitivity
      - sensor.attic_motion_sensor_sensitivity

  hue_threshold_sensors:
    name: Hue threshold sensors
    control: hidden
    icon: mdi:numeric
    entities:
      - sensor.laundry_motion_sensor_threshold
      - sensor.dining_table_motion_sensor_threshold
      - sensor.auditorium_motion_sensor_threshold
      - sensor.frontdoor_motion_sensor_threshold
      - sensor.dorm_motion_sensor_threshold
      - sensor.corridor_motion_sensor_threshold
      - sensor.corridor_terrace_motion_sensor_threshold
      - sensor.master_bedroom_motion_sensor_threshold
      - sensor.corridor_office_motion_sensor_threshold
      - sensor.control_room_motion_sensor_threshold
      - sensor.attic_motion_sensor_threshold

##############################################################################################################
# Notes
##############################################################################################################
1 Like

as an update:

Above scripts work fine, and setting the input selects is great as an interface with manual input.

Had the need to use these in automation directly with fixed values for some of my scenes.

So created a ‘direct’ script:

  set_hue_sensitivity_direct:
    alias: Set Hue sensitivity direct
    sequence:
      service: rest_command.set_hue_command
      data_template:
        command: config
        type: sensors
        data: >
          {"sensitivity": {{sensitivity}} }
        id: >
          {{id}}

tested it in the dev-service, with data

{"sensitivity": 0,"id":44} my motion sensors gets its sensitivity set accordingly. So I know this works as designed.

using this in the scenes now:

- service: script.set_hue_sensitivity_direct
  data_template:
    id: 44
    sensitivity: 0

sets the sensors accordingly. Which is great, cause I can now do it manually with the input_selects, and in an automation setting sensor and sensitivity directly :wink:

Cross-link: Please find a similar example of a template sensor here

Best

It looks great! How do you install this?
:slight_smile:

It’s all in the post? Not sure what else you need? Did you give it a go already? If yes, what’s the result, let me know so we can help

1 Like

I have been getting into Home Assistant for 6 months now and I love it. I know my way pretty well, as a non-coder but with some technical background. Can’t seem to get where to start with ‘installing’ your great component. Does it need to be uploaded into custom components ? I am sorry, maybe I’m overlooking the obvious. I appreciate your help! Thanks

if your talking about the custom integration for Hue sensors, it isn’t mine but @robmarkcole 's, and yes, that needs to go into custom_components. It is under heavy development as we speak, so Yule got to be careful now…

my other files are in a package an can simply be added in the folder /config/packages after you have enabled package support in your configuration.yaml see: https://www.home-assistant.io/docs/configuration/packages/

some time has passed since I wrote this package, so must check if all is still up to date with the latest breaking changes in HA. Thing is the interface bits in this package were written for HA States, and not for Lovelace. I can help you there, but first you might give the backend stuff a go, which is what you need to start with.

1 Like

Thanks. Haven’t gotten into packages yet. I will try this out, as soon as I have some time.
Would be awesome to get Hue accessories configuration integrated into HA core, using the interface from your package!

1 Like