How to incorporate "attributes" of a state (iPhone) into basic Node-Red automations

I don’t understand how to incorporate attributes to an entity’s state into my automations. For instance, here is my phone. The “state” is “Office” because I am in my geofenced location for work:

source_type: gps
battery_level: 38
latitude: xxxxx
longitude: -xxxxx
gps_accuracy: 32.00483309472351
attribution: Data provided by Apple iCloud
account_fetch_interval: 5
device_name: iPhone X
device_status: offline
owner_fullname: xxxxxx
battery_status: NotCharging
battery: 38
low_power_mode: false
friendly_name: Steve's Phone
icon: 'mdi:cellphone-iphone'

I am curious how I am able to incorporate some of these attributes into my automations using node-red (I really like the visualization of the automation flows that provides me! Great for someone as inexperienced as me). For instance, how would I go about changing my desk lamp from color to color based on the following:
if battery is over under 20, light is red at 100% brightness
if battery is over 20-98 and is not currently charging, light is green at 20% brightness
if battery is over 20-98 and is currently charging, light does a colorloop at 100% brightness
if battery is 99-100, light is green, at 100% brightness

These automations would also require me giving the light.turn_on service more details (color, brightness, colorloop effect, etc)

Thank you so much in advance. Outstanding product this is.

1 Like

Something like this …

in sensors:

- platform: template 
  sensors: 
    iphone_tim_battery_level:
      value_template: "{{ state_attr('device_tracker.tims_iphone_2','battery_level') }}"
      friendly_name: iPhone Tim Battery Level

in your view in lovelace ui:

          - type: "custom:button-card"
            entity: sensor.iphone_tim_battery_level
            styles:
              custom_fields:
                bat_level:
                  - padding-bottom: 1px
                  - align-self: middle
                  - justify-self: start
                  - --text-color-sensor: >
                      [[[ 
                        if (states["sensor.iphone_tim_battery_level"].state > 80) 
                          return "red";
                        else if ((states["sensor.iphone_tim_battery_level"].state <= 80) && (states["sensor.iphone_tim_battery_level"].state > 60))
                          return "orange"; 
                        else if ((states["sensor.iphone_tim_battery_level"].state <= 60) && (states["sensor.iphone_tim_battery_level"].state > 20))
                          return "#b0f0b0"; 
                        else
                          return "deepskyblue";
                      ]]]
            custom_fields:
              bat_level: '[[[ return `<span> Battery: <span style="color: var(--text-color-sensor);">${states["sensor.iphone_tim_battery_level"].state}</span></span>` ]]]'

there are endless ways of doing it in Node-red but I would do something like the following.

[{"id":"9e8560a.df623a","type":"trigger-state","z":"110b2c7b.9890f4","name":"battery level","server":"e447d17a.16a64","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.serkans_iphone_battery_level","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"num","x":150,"y":1000,"wires":[["7cf83645.fbc228"],[]]},{"id":"7cf83645.fbc228","type":"switch","z":"110b2c7b.9890f4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"20","vt":"num"},{"t":"btwn","v":"20","vt":"num","v2":"98","v2t":"num"},{"t":"gt","v":"98","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":330,"y":1000,"wires":[["98a1168f.595548"],["e7ac4a8c.87fce8"],[]]},{"id":"e7ac4a8c.87fce8","type":"switch","z":"110b2c7b.9890f4","name":"","property":"data.event.new_state.attributes[\"Battery State\"]","propertyType":"msg","rules":[{"t":"eq","v":"Not Charging","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":1000,"wires":[["9ffdb7af.1e1398"],[]]},{"id":"98a1168f.595548","type":"api-call-service","z":"110b2c7b.9890f4","name":"light red","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room_middle","data":"{\"brightness\":100,\"color_name\":\"red\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":620,"y":940,"wires":[[]]},{"id":"9ffdb7af.1e1398","type":"api-call-service","z":"110b2c7b.9890f4","name":"light green","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room_middle","data":"{\"brightness\":20,\"color_name\":\"green\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":670,"y":980,"wires":[[]]},{"id":"e447d17a.16a64","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

2 switch nodes, first direct the node based on battery level and 2nd to evaluate if it is charging or not.

sorry too lazy to put all service call nodes but you can complete other 2 conditions